package com.ztesoft.ossup.report.dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.util.StringUtils;

import com.ztesoft.ossup.report.domain.ReportChartData;
import com.ztesoft.ossup.report.domain.ReportChartData_;

public interface ChartDataRepo extends JpaRepository<ReportChartData, Long>, JpaSpecificationExecutor<ReportChartData> {
	class ReportSpecifications {

		public static Specification<ReportChartData> likeReportName(final String title) {
			return new Specification<ReportChartData>() {
				@Override
				public Predicate toPredicate(Root<ReportChartData> root, CriteriaQuery<?> criteriaQuery,
						CriteriaBuilder criteriaBuilder) {
							if (!StringUtils.isEmpty(title))
								return criteriaBuilder.like(root.get(ReportChartData_.label), "%" + title + "%");
							return null;
						}
			};
		}
	}
	List<ReportChartData> findAllByChartId(Long chartId);
	
	@Query(value="delete from report_chart_data T WHERE T.CHART_ID in (select id from report_chart where report_id = ?1)",nativeQuery = true)
	@Modifying
	void deleteChartDataByReportId(Long reportId);
} 
